package cn.wolfcode.controller;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 普发忠
 * @Date: 2021/11/07/9:48
 */
@RestController
public class SendMessageController {
    @Autowired
    private RocketMQTemplate mqTemplate;
    
    @RequestMapping("/sendMsg")
    public String  sendMessage(String message){
        //同步消息
//        SendResult sendResult = mqTemplate.syncSend("01-boot-topic", message);
//        System.out.println(sendResult.getSendStatus());
        
        //异步消息
//        mqTemplate.asyncSend("01-boot-topic", message, new SendCallback() {
//            @Override
//            public void onSuccess(SendResult sendResult) {
//                System.out.println("发送成功");
//            }
//
//            @Override
//            public void onException(Throwable throwable) {
//                System.out.println("消息发送失败");
//            }
//        });
        
        //一次性消息
     // mqTemplate.sendOneWay("01-boot-topic", message);
    
    //延时消息
      //  mqTemplate.syncSend("01-boot-topic", MessageBuilder.withPayload(message).build(),5000,3);
     //使用不同的消费组
    //        mqTemplate.convertAndSend("01-boot-topic:TagA", message+"A");
    //        mqTemplate.convertAndSend("01-boot-topic:TagB", message+"B");


        //sql 92
        Map<String,Object> map=new HashMap<>();
        //用户自定义属性
        map.put("age", 15);
        map.put("name", "小明");
        //也可以设置系统属性
        Map<String,Object> map2=new HashMap<>();
        //用户自定义属性
        map2.put("age", 20);
        map2.put("name", "小红");
        //也可以设置系统属性
        
        mqTemplate.convertAndSend("01-boot-topic", message+"20岁",map2);
        mqTemplate.convertAndSend("01-boot-topic", message+"15岁",map);
    
        return "成功";
  
        
    }
}
